package com.hp.bon.sgw.wh.mml;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.bon.sgw.wh.mml.common.AckEntity;
import com.hp.bon.sgw.wh.mml.common.MMLConstants;

public class MMLDecoder {
	
	private static final Logger logger = LoggerFactory.getLogger(MMLDecoder.class);
	
	public static AckEntity parseMMLCommand(String vcResponse){
		try {
			return parseMMLCommandInner(vcResponse);
		} catch(Exception e){
			logger.warn("parseMMLCommand error: ", e);
		}
		return null;
	}
	
	public static AckEntity parseMMLCommandInner(String vcResponse){
		if(!commonCheck(vcResponse)){
			logger.warn("vcResponse commonCheck fail!!!");
			return null;
		}
		vcResponse = vcResponse.trim();
		String mmlCommand = null;
		mmlCommand = vcResponse.substring(MMLConstants.MML_COMMAND_BEGIN, vcResponse.length() - MMLConstants.MML_CHECK_SUM_LEN);
		//mmlCommand = mmlCommand.trim().replaceAll(MMLConstants.KEY_BLANK, "");
		//mmlCommand = mmlCommand.trim();
		logger.info("vcResponse mmlCommand=" + mmlCommand);
		if(!mmlCommand.startsWith(MMLConstants.MML_ACK_START_WORD)){
			return null;
		}
		
		AckEntity ackEntity = new AckEntity();
		//login ACK
		if (mmlCommand.indexOf(MMLConstants.CMD_LOGIN) > 0) {
			ackEntity.setAckType(AckEntity.TYPE_LOGIN);
		} else if(mmlCommand.indexOf(MMLConstants.CMD_LOGOUT) > 0){
			ackEntity.setAckType(AckEntity.TYPE_LOGOUT);
		} else if(mmlCommand.indexOf(MMLConstants.CMD_RECH_EVC_PPSUSER) > 0
				|| mmlCommand.indexOf(MMLConstants.CMD_LIST_EVC_TERMUSER) > 0
				|| mmlCommand.indexOf(MMLConstants.CMD_CANCEL_EVC_RECH) > 0
				|| mmlCommand.indexOf(MMLConstants.CMD_LIST_EVC_STK) > 0
				){
			ackEntity.setAckType(AckEntity.TYPE_TRANSACTION);
		}
		
//		mmlCommand = mmlCommand.replaceAll(MMLConstants.KEY_BLANK, "");
//		int index = mmlCommand.lastIndexOf(':');
//		if(index>0 && index<mmlCommand.length()){
//			mmlCommand = mmlCommand.substring(index+1);
//		}
		List<String> itemsList = Arrays.asList(mmlCommand.split(MMLConstants.MML_SEPARATOR));
		for(String item : itemsList){
			if (item == null || item.trim().isEmpty() || item.indexOf(MMLConstants.MML_EQUALITY) <= 0) {
				continue;
			}
			String[] tmpArr = item.split(MMLConstants.MML_EQUALITY);
			String name = tmpArr[0].trim();
			int index = name.lastIndexOf(':');
			if(index>0){
				name = name.substring(index+1);
			}			
			String value = tmpArr[1].trim();
			if(value.length()>2){
				if(value.startsWith("\"")){
					value = value.substring(1);
				}
				if(value.endsWith("\"")){
					value = value.substring(0, value.length()-1);
				}
			}		
			ackEntity.addAckItem(name.trim(), value.trim());
		}
		return ackEntity;
	}
	
	public static String parseSessionId(String vcResponse){		
		if(!commonCheck(vcResponse)){
			return null;
		}
		vcResponse = vcResponse.trim();
		String sessionId = null;
		sessionId = vcResponse.substring(MMLConstants.MML_SESSION_ID_BEGIN, MMLConstants.MML_SESSION_ID_END);
		return sessionId.toUpperCase();
	}
	
	public static String parseVCHearBeat(String vcResponse){
		if(vcResponse==null){
			return null;
		}
		vcResponse = vcResponse.trim();
		if(vcResponse.length()!=MMLConstants.MML_HEART_BEAT_LEN){
			return null;
		}
		String heartbeat = vcResponse.substring(8, 12);
		if(heartbeat.equalsIgnoreCase(MMLConstants.MML_HEART_BEAT_BODY)){
			return vcResponse;
		}
		return null;
	}
	
	public static boolean commonCheck(String vcResponse){
		if(vcResponse==null || vcResponse.length()<80){
			return false;
		}
		return true;
	}
}
